<?php 
class Controller_News_View extends ypController {
	public function Index() {
		$this->Loader->model('News/Index');
		$this->Loader->model('News/View');
		
		// Load post ID
		$id = 0;
		if ((int) $this->Setting->get('post_request_type') == 1) {
			$alias = $this->Request->GET('alias');
			$alias = preg_replace('/(\/)*$/', '', trim($alias));
			$explode = explode('/', $alias);
			if (!empty($explode)) {
				$post = array_pop($explode);
				$id = $this->Model_News_View->loadPostIdFromAlias($post);
				$this->Request->GET['id'] = $id; // fix for comment, it get post_id but in url not exists Id param
			}
		} else {
			$id = isset($this->Request->GET['id']) ? intval($this->Request->GET['id']) : 0;
		}

		if ($id == 0) {
			die('Error: Post not exists!');
			//$this->redirect($this->Link->build('Index/Home', TRUE));
		}

 		$this->loadLanguage('News/View');

		// Load news 
		$news = $this->Model_News_View->load($id);

		$this->Model_News_Index->createBreadcrumb($news['cat_id'], $news['title']);

 		if (!$news) {
			$this->setTemplate('module/News/NotFound.php');
			$this->Document->setTitle(sprintf($this->_data['title'], $this->_data['text_not_found']));
			$this->setTemplateChild(array(
				'System/Header',
				'System/Footer',
			));
			$this->Response->setOutput($this->render());
			return;
		}

		$this->Model_News_View->updateCounter($id);

		$news['post_date'] = $news['post_date'] ? date('d/m/Y h:i', $news['post_date']) : '';
		$news['post'] = htmlspecialchars_decode($news['post']);
		$news['post_by'] = $this->Link->a(array('User/User', array('userid' => $news['user_id'])), $news['username']);
		if ($this->Setting->get('show_view_counter') AND intval($this->Setting->get('show_view_counter')) == 0) { 
			$news['view_count'] = FALSE;
		} else {
			$news['view_count'] = sprintf($this->_data['text_view_count'], $news['view_count']);
		}
		if ($this->Setting->get('like_active') AND intval($this->Setting->get('like_active')) == 0) {
			$news['like_info'] = FALSE;
		} else {
			$news['like_info'] = sprintf($this->_data['text_like_count'], $news['like_count']);
		}
		$this->Document->setKeywork($news['keywork']);
		$this->set('addthis', $this->Setting->get('addthis'));

		// Tag 
		$tags = $this->Model_News_View->getTags($news['post_id']);
		if (!$tags) {
			$news['tags'] = $this->_data['text_no_tag'];
		} else {
			$news['tags'] = array();
			foreach($tags as $tag) {
				$news['tags'][] = $this->Link->a(array('News/Tag', array('tag' => $tag)), $tag);
			}
			$news['tags'] = implode(', ', $news['tags']);
		}

		// Link 
		$this->_data['link_edit'] = $this->Link->build('Admin/Module/News/Edit', TRUE, array('id' => $news['post_id']));
		$this->_data['link_like'] = $this->Link->build('News/View/Like', TRUE, array('id' => $news['post_id'], 'user_id' => $this->User->get('user_id')));
		$this->_data['link_edit'] = $this->Link->build('Admin/Module/News/Edit', TRUE, array('id' => $news['post_id']));

		// Quick editor 
		$this->set('quick_edit', FALSE);
		if ($this->User->isAdmin() AND $this->Setting->get('quick_edit')) { 
			$this->set('quick_edit', TRUE);
		}

		$this->setTemplate('module/News/View.php');
		$this->setTemplateChild(array(
			'System/Header',
			'System/Footer',
			'News/Comment',
			'News/Comment/ListComment',
			'System/Breadcrumb',
		));
		$this->Document->setTitle(sprintf($this->_data['title'], $news['title']));
		$this->set('news', $news);
		$this->Response->setOutput($this->render());
	}
}